package com.visikard.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.visikard.item.ItemGiftKardRedeem;
import com.visikard.ui.R;
import com.visikard.ui.fragment.RingUpRedeemFragment;
import com.visikard.ui.fragment.ViewClickable;

public class RingUpRedeemActivity extends BaseFragmentActivity implements ViewClickable {

	public static final String EXT_GIFT_KARD_ITEM = "extrasGiftKarditem";

	private FragmentManager fmManager;
	private FragmentTransaction fmTransaction;

	private ItemGiftKardRedeem mListgiftKards;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.ringup_redeem_activity);
		fmManager = getSupportFragmentManager();

		mListgiftKards = (ItemGiftKardRedeem) getIntent().getSerializableExtra(EXT_GIFT_KARD_ITEM);

		RingUpRedeemFragment ringUp = new RingUpRedeemFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable(RingUpRedeemFragment.EXT_ITEM_GIFT_KARD, mListgiftKards);

		ringUp.setArguments(bundle);
		putFragment(ringUp);
	}

	/**
	 * put a fragment with UI
	 */
	public void putFragment(Fragment fm) {
		fmTransaction = fmManager.beginTransaction();
		fmTransaction.setCustomAnimations(R.anim.push_left_in, 0, 0, R.anim.push_right_out);
		fmTransaction.add(R.id.startup_root, fm);
		fmTransaction.addToBackStack(null);
		fmTransaction.commit();
	}

	public void finishActivity() {
		finish();
		overridePendingTransition(R.anim.anim_slide_in_from_left, R.anim.anim_slide_out_to_right);
	}

	@Override
	public void onBackPressed() {
		finishActivity();
	}

	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
			default:
				try {
					Fragment fm = fmManager.findFragmentById(R.id.startup_root);
					if (fm instanceof ViewClickable) {
						((ViewClickable) fm).onViewClick(v);
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				break;
		}
	}

}
